<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title><?php echo $title;?></title>
<link href="<?php echo base_url(); ?>assets/public/css/reset.css" media="screen" rel="stylesheet" type="text/css" />
<link href="<?php echo base_url(); ?>assets/public/css/style.css" media="screen" rel="stylesheet" type="text/css" />
<!--[if IE]>
<link href="css/ie.css" media="screen" rel="stylesheet" type="text/css">
<![endif]-->
<?php 
foreach($css_files as $file): ?>
	<link type="text/css" rel="stylesheet" href="<?php echo $file; ?>" />
<?php endforeach; ?>
<style type="text/css">
    .movebtn button {
        margin: 10px 0px;
    }
    #box1View option {
        width: auto;
    }
</style>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/public/js/jquery-1.9.1.js"></script>
<?php foreach($js_files as $file): ?>
<script type="text/javascript" src="<?php echo $file; ?>"></script>
<?php endforeach; ?>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/public/js/jquery-tipsy.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/public/js/jquery-custom.js"></script>
<script type="text/javascript" language="javascript">
        $(document).ready(function(){
           
        });
        $(function() {

            $.configureBoxes();

        });
        
        function clear_all(){
             $("#box1View").find('option').remove();
             $("#box2View").find('option').remove();
             $("#des_resources").find('option').remove();
             $("#des_materials").find('option').remove();
        }
        var workid = '0';
</script>
</head>
<body>
<div class="shadow"></div>
<!-- .shadow -->
 <?php $this->load->view("common/header_view"); ?>
 <?php $this->load->view("common/sidebar_view",$sb);?>
<!-- BEGIN CONTENT -->
<div id="content">
    <div class="box-out">
    	<div class="box-in">
    		<div class="box-head"><h1><?php echo $content_title;?></h1></div>
                <div class="box-content" id="personnel_content">
                    <!--*********************Layout content********************************-->
                    <div class="form">
                        <form name="assign_personnel" method="post" action="<?php echo base_url();?>projects/assign_personnels_process/<?php echo $pj_id; ?>">  
                        <label>Phân công</label>
                        <select name="bossid" class="text" id="personnels_assign">
                            <option value="0">Chọn phân công</option>
                             <?php echo arrTooptions($managers,'managername','id') ?>
                        </select>
                        
                        <script type="text/javascript">
                            $('#personnels_assign').change(function(){
                                    var manager_id = $('#personnels_assign').val();
                                    var ischange = 0;
                                    var pj_id = <?php echo $pj_id;?>;
                                    //Kiem tra trang thai du an xem nhom truong da phan cong chua,1 la chua, 2 la da
                                    $.ajax({
                                    url: <?php echo "'".  base_url()."projects/ajax_check_assign/"."'";?>+manager_id,
                                    type:'POST',
                                    dataType: 'json',
                                    success: function(output_string){
                                               if(output_string!='2') ischange =1;                   
                                        } 
                                    });//End ajax
                                    clear_all();
                                    if(manager_id!=0) {
                                        $.ajax({
                                        url: <?php echo "'".  base_url()."projects/ajax_load_all/"."'";?>+pj_id+'/'+manager_id,
                                        type:'POST',
                                        dataType: 'json',
                                        success: function(output_string){

                                             /**
                                                - Dinh dang : li pn chua phan cong ------li pn da phan cong------
                                                              li nguyen lieu da phan cong-----
                                                              li tai nguyen da phan cong
                                                              ------ workid
                                             **/
                                             var arr = output_string.split('------');
                                             workid = arr[4];
                                             $("#box1View").find('option').remove().end().append(arr[0]);
                                             $("#box2View").find('option').remove().end().append(arr[1]);
                                             $("#des_resources").find('option').remove().end().append(arr[2]);
                                             $("#des_materials").find('option').remove().end().append(arr[3]);

                                            } 
                                        });//End ajax
                                    }else {
                                        
                                    }
                                    
                                    
                                    /***
                                    // Load box ben da phan cong
                                    $.ajax({
                                    url: <?php echo "'".  base_url()."projects/ajax_get_pn_of_manager/"."'";?>+manager_id,
                                    type:'POST',
                                    dataType: 'json',
                                    success: function(output_string){
                                            $("#box2View").find('option').remove().end().append(output_string);
                                        } 
                                    });//End ajax
                                    
                                    // Load boss ben chua phan cong
                                    $.ajax({
                                    <?php if(is_admin()||is_pm($pj_id)) { ?>
                                    url: <?php echo "'".  base_url()."projects/ajax_get_pn_free_manager/"."'";?>+manager_id,
                                    <?php } else { ?>
                                    url: <?php echo "'".  base_url()."projects/ajax_get_pn_of_parentmanager/"."'";?>+manager_id,
                                    <?php } ?> 
                                    type:'POST',
                                    dataType: 'json',
                                    success: function(output_string){
                                            $("#box1View").find('option').remove().end().append(output_string);
                                        } 
                                    });//End ajax
                                    
                                    //Load nguyen lieu va tai nguyen dc phan cong cho du an va nhom truong
                                    $.ajax({
                                    url: <?php echo "'".  base_url()."projects/ajax_get_resources_materials_of_pn_project/"."'";?>+pn_id+'/'+pj_id,
                                    type:'POST',
                                    dataType: 'json',
                                    success: function(output_string){
                                            var arr = output_string.split('------');
                                            $("#des_resources").find('option').remove().end().append(arr[0]);
                                            $("#des_materials").find('option').remove().end().append(arr[1]);
                                    } 
                                    });//End ajax
                                    ****/
                            });
                        </script>
                        <label>Phân công</label>
                        <div style="font-size: .8em">
                            <table>
                                            <tr>
                                            <td>
                                                    Lọc: <input type="text" id="box1Filter" /><button type="button" id="box1Clear">X</button><br />
                                                    <select id="box1View" multiple="multiple" style="height:300px;width:300px;font-size: 1em">
                                                        
                                                    </select><br/>
                                                    <select id="box1Storage">

                                                    </select>
                                            </td>
                                                <td style="width: 60px;margin: 0px 10px;text-align: center;vertical-align: middle" class="movebtn">
                                                <button id="to2" type="button">&nbsp;>&nbsp;</button>
                                                <button id="allTo2" type="button">&nbsp;>>&nbsp;</button>
                                                <button id="allTo1" type="button">&nbsp;<<&nbsp;</button>
                                                <button id="to1" type="button">&nbsp;<&nbsp;</button>
                                            </td>
                                            <td>
                                                Lọc: <input type="text" id="box2Filter" /><button type="button" id="box2Clear">X</button><br />
                                                <select name="pns" id="box2View" multiple="multiple" style="height:300px;width:300px;font-size: .9em">
                                                </select><br/>
                                                <select id="box2Storage">
                                                </select>
                                            </td>
                                        </tr>
                            </table>
                        </div>
                        <fieldset style="width: 636px;border-color:#E0E2FF;">
                            <legend>Tài nguyên</legend>
                            <select id="des_resources" multiple="multiple" style="height:200px;width:630px;font-size: .8em">
                                
                            </select>
                            <p>
                                <select id="re_resources">
                                    
                                    <?php
                                      echo arrTooptions($resources,'name','id');
                                    ?>
                                </select>
                                <?php //print_r($resources); ?>
                                <span>Số lượng</span><input style="width: 40px" type="text" id="amount"/><input type="button" id="btn_add_resources" value="Thêm"/><input type="button" id="btn_remove" value="Xóa"/>
                                <script>
                                    $('#btn_add_resources').click(function(){
                                        var id = $('#re_resources').val();
                                        var add_amount = $('#amount').val().trim();
                                        if(Math.floor(add_amount) == add_amount && $.isNumeric(add_amount)){
                                            var f =  $('#des_resources option[value="' + id + '"]').html();
                                            var text = $("#re_resources :selected").text()+', Số lượng ';
                                            var add_amount = $('#amount').val();
                                            if(f!=null){// neu tai nguyen chua dc chon
                                                add_amount = parseInt(add_amount);
                                                var amount = parseInt($('#des_resources option[value="' + id + '"]').attr('amount'));
                                                $('#des_resources option[value="' + id + '"]').removeAttr('amount');
                                                $('#des_resources option[value="' + id + '"]').attr('amount',amount+add_amount);
                                                var str_amount = amount+add_amount+'';
                                                $('#des_resources option[value="' + id + '"]').html(text + str_amount);
                                            }else {
                                            // $('#des_resources").append();
                                             $('#des_resources').append($('<option>', { 
                                                value: id,
                                                text : text+''+add_amount,
                                                amount:add_amount
                                              }));
                                            }
                                        }else {
                                            alert("Số lượng phải kiểu số !");
                                        }
                                    });
                                    $('#btn_remove').click(function(){;
                                        $("#des_resources option:selected").remove();
                                    });
                                </script>
                            </p>
                        </fieldset>
                        <fieldset style="width: 636px;border-color:#E0E2FF">
                            <legend>Nguyên liệu</legend>
                            <select multiple="multiple" id="des_materials" style="height:200px;width:630px;font-size: 0.8em">
                               
                            </select>
                            <p>
                                <select id="re_materials">
                                    <?php
                                      echo arrTooptions2($materials,'name','id','unit','unit');
                                    ?>
                                </select>
                                <span>Số lượng</span><input style="width: 40px" id="amount2" type="text" id="amount"/><input type="button" id="btn_add_materials" value="Thêm"/><input type="button" id="btn_remove2" value="Xóa"/>
                                <script>
                                    $('#btn_add_materials').click(function(){
                                        var add_amount = $('#amount2').val().trim();
                                        if(Math.floor(add_amount) == add_amount && $.isNumeric(add_amount)){
                                            var id = $('#re_materials').val();
                                            var f =  $('#des_materials option[value="' + id + '"]').html();
                                            var text = $("#re_materials :selected").text()+', Số lượng ';
                                            var unit = $("#re_materials :selected").attr('unit');
                                            if(f!=null){// neu tai nguyen chua dc chon
                                                add_amount = parseInt(add_amount);
                                                var amount = parseInt($('#des_materials option[value="' + id + '"]').attr('amount'));
                                                $('#des_materials option[value="' + id + '"]').removeAttr('amount');
                                                $('#des_materials option[value="' + id + '"]').attr('amount',amount+add_amount);
                                                var str_amount = amount+add_amount+'';
                                                $('#des_materials option[value="' + id + '"]').html(text + str_amount+' '+unit);
                                            }else {
                                             $('#des_materials').append($('<option>', { 
                                                value: id,
                                                text : text+''+add_amount+' '+unit,
                                                amount:add_amount
                                              }));
                                            }
                                        }else {alert('Vui lòng nhập số lượng là kiểu số !')}
                                    });
                                    $('#btn_remove2').click(function(){;
                                        $("#des_materials option:selected").remove();
                                    });
                                </script>
                            </p>
                        </fieldset>
                        
                        <br/>
                        <input type="button" value="Lưu lại" ata-reveal-id="myModal" class="submit" name="btnupdate" id="btnupdate"/>
                        <script>
                          $('#btnupdate').click(function(e){     
                                if($('#personnels_assign').val()!=0){
                                    var is_busy = 0;
                                    var str = '';
                                    $("#box2View option").each(function(){
                                         str += '---'+$(this).val();
                                         if($(this).attr('title')!=null) is_busy=1;
                                    });
                                    str = str.substring(3);

                                    var _pj_id = <?php echo $pj_id;?>;

                                    var _managerid = $('#personnels_assign').val();

                                    var _str_re ='';
                                    $("#des_resources option").each(function(){
                                             _str_re += '---'+$(this).val()+'--'+$(this).attr('amount');;
                                    });
                                    _str_re = _str_re.substring(3);

                                    var _str_ma ='';
                                    $("#des_materials option").each(function(){
                                         _str_ma += '---'+$(this).val()+'--'+$(this).attr('amount');
                                    });
                                    _str_ma = _str_ma.substring(3);

                                    var form_data = {
                                        pj_id : _pj_id,
                                        managerid : _managerid,
                                        mydata : str,
                                        str_re : _str_re,
                                        str_ma : _str_ma,
                                        is_ajax : 1
                                    }; 
                                  //  alert('is_busy '+is_busy+'--workid'+workid);
                                   if(is_busy==1&&workid!='') {// Neu nhu co nguoi ban    
                                        $("#box2View option").each(function(){
                                            var w_pn = $(this).val();
                                            var pj_id = <?php echo $pj_id; ?>;
                                            var url = <?php echo "'".base_url()."managers/check/'"; ?>+pj_id+'/'+w_pn+'/'+workid;
                                            $('#div_img').append("<img src='"+url+"' /><br/>");
                                        });
                                       e.preventDefault();
                                       $('#myModal').reveal();
                                    }else {   
                                        // Them
                                          $.ajax({
                                            type: "POST",
                                            url: "<?php echo base_url().'projects/ajax_assign_personnels_process/'; ?>",
                                            data: form_data,
                                            success: function(response)
                                            {
                                               alert(response);
                                            }
                                         });   
                                         //alert(is_busy);
                                    }
                                                            
                                }else {
                                    alert('Chọn phân công !');
                                }
                          });
                        </script>
                    </form>
                 </div>
                    <!--*********************End layout content****************************-->
                </div>
        </div>
    </div>
</div>
<!--- END CONTENT --->
 <div id="myModal" class="reveal-modal">
			<h1>Bảng so sánh công việc</h1>	<br/>		
                        <div style="overflow: scroll" id="div_img">
                            
                        </div>
                        <p style="margin-top: 10px"><input id="btndoadd" class="mysubmit" type="button" value="Đồng ý" /></p>
                        <script type="text/javascript">
                            $("#btndoadd").click(function(){
                                var str = '';
                                $("#box2View option").each(function(){
                                     str += '---'+$(this).val();
                                });
                                str = str.substring(3);

                                var _pj_id = <?php echo $pj_id;?>;

                                var _managerid = $('#personnels_assign').val();

                                var _str_re ='';
                                $("#des_resources option").each(function(){
                                         _str_re += '---'+$(this).val()+'--'+$(this).attr('amount');;
                                });
                                _str_re = _str_re.substring(3);

                                var _str_ma ='';
                                $("#des_materials option").each(function(){
                                     _str_ma += '---'+$(this).val()+'--'+$(this).attr('amount');
                                });
                                _str_ma = _str_ma.substring(3);

                                var form_data = {
                                    pj_id : _pj_id,
                                    managerid : _managerid,
                                    mydata : str,
                                    str_re : _str_re,
                                    str_ma : _str_ma,
                                    is_ajax : 1
                                }; 
                                // Them
                                 $.ajax({
                                    type: "POST",
                                    url: "<?php echo base_url().'projects/ajax_assign_personnels_process/'; ?>",
                                    data: form_data,
                                    success: function(response)
                                    {
                                       alert(response);
                                    }
                                 });    
                            });
                        </script>
                        <a class="close-reveal-modal">&#215;</a>
</div>
</body>
</html>
